#include "systemconfig.h"
#include <QDebug>

SystemConfig::SystemConfig(const QString &fileName, QObject *parent)
    : IniConfig (fileName, parent)
{
    m_configIDMap.clear();
    m_configIDMap.insert(CK_Uart_AutoOpen,    					{"uart",         		"auto_open",                false});
    m_configIDMap.insert(CK_Uart_Name,                  		{"uart",                "name",                     "COM4"});
    m_configIDMap.insert(CK_Uart_BaudRate,              		{"uart",                "baud_rate",                "115200"});
    m_configIDMap.insert(CK_Uart_DataBits,              		{"uart",                "data_bits",                "8"});
    m_configIDMap.insert(CK_Uart_StopBits,              		{"uart",                "stop_bits",                "1"});
    m_configIDMap.insert(CK_Uart_Parity,                		{"uart",                "parity",                   "None"});

    m_configIDMap.insert(CK_DebugUart_AutoOpen,    				{"debug_uart",   		"auto_open",                false});
    m_configIDMap.insert(CK_DebugUart_Name,                     {"debug_uart",          "name",                     "COM2"});
    m_configIDMap.insert(CK_DebugUart_BaudRate,                 {"debug_uart",          "baud_rate",                "115200"});
    m_configIDMap.insert(CK_DebugUart_DataBits,              	{"debug_uart",          "data_bits",                "8"});
    m_configIDMap.insert(CK_DebugUart_StopBits,              	{"debug_uart",          "stop_bits",                "1"});
    m_configIDMap.insert(CK_DebugUart_Parity,                	{"debug_uart",          "parity",                   "None"});

    for (int i = 0; i < 4; i++)
    {
        int offset = 6 * i;
        QString sector = QString("pump%1_uart").arg(QChar('a' + i));
        m_configIDMap.insert((ConfigKey)(CK_PumpAUart_AutoOpen + offset),    			{sector,    	   "auto_open",                false});
        m_configIDMap.insert((ConfigKey)(CK_PumpAUart_Name + offset),                  	{sector,           "name",                     "COM4"});
        m_configIDMap.insert((ConfigKey)(CK_PumpAUart_BaudRate + offset),              	{sector,           "baud_rate",                "115200"});
        m_configIDMap.insert((ConfigKey)(CK_PumpAUart_DataBits + offset),              	{sector,           "data_bits",                "8"});
        m_configIDMap.insert((ConfigKey)(CK_PumpAUart_StopBits + offset),              	{sector,           "stop_bits",                "1"});
        m_configIDMap.insert((ConfigKey)(CK_PumpAUart_Parity + offset),                	{sector,           "parity",                   "None"});
    }

    m_configIDMap.insert(CK_DetectorUart_AutoOpen,    				{"detector_uart",    		"auto_open",                false});
    m_configIDMap.insert(CK_DetectorUart_Name,                  	{"detector_uart",           "name",                     "COM4"});
    m_configIDMap.insert(CK_DetectorUart_BaudRate,              	{"detector_uart",           "baud_rate",                "115200"});
    m_configIDMap.insert(CK_DetectorUart_DataBits,              	{"detector_uart",           "data_bits",                "8"});
    m_configIDMap.insert(CK_DetectorUart_StopBits,              	{"detector_uart",           "stop_bits",                "1"});
    m_configIDMap.insert(CK_DetectorUart_Parity,                	{"detector_uart",           "parity",                   "None"});

    m_configIDMap.insert(CK_CollectorUart_AutoOpen,    				{"collector_uart",    		"auto_open",                false});
    m_configIDMap.insert(CK_CollectorUart_Name,                  	{"collector_uart",           "name",                     "COM4"});
    m_configIDMap.insert(CK_CollectorUart_BaudRate,              	{"collector_uart",           "baud_rate",                "115200"});
    m_configIDMap.insert(CK_CollectorUart_DataBits,              	{"collector_uart",           "data_bits",                "8"});
    m_configIDMap.insert(CK_CollectorUart_StopBits,              	{"collector_uart",           "stop_bits",                "1"});
    m_configIDMap.insert(CK_CollectorUart_Parity,                	{"collector_uart",           "parity",                   "None"});

    m_configIDMap.insert(CK_AutoSamplerUart_AutoOpen,    			{"autosampler_uart",    	"auto_open",                false});
    m_configIDMap.insert(CK_AutoSamplerUart_Name,                  	{"autosampler_uart",           "name",                     "COM4"});
    m_configIDMap.insert(CK_AutoSamplerUart_BaudRate,              	{"autosampler_uart",           "baud_rate",                "115200"});
    m_configIDMap.insert(CK_AutoSamplerUart_DataBits,              	{"autosampler_uart",           "data_bits",                "8"});
    m_configIDMap.insert(CK_AutoSamplerUart_StopBits,              	{"autosampler_uart",           "stop_bits",                "1"});
    m_configIDMap.insert(CK_AutoSamplerUart_Parity,                	{"autosampler_uart",           "parity",                   "None"});

    m_configIDMap.insert(CK_SimpleFocUart_AutoOpen,    				{"simplefoc_uart",    		"auto_open",                false});
    m_configIDMap.insert(CK_SimpleFocUart_Name,                  	{"simplefoc_uart",           "name",                     "COM4"});
    m_configIDMap.insert(CK_SimpleFocUart_BaudRate,              	{"simplefoc_uart",           "baud_rate",                "115200"});
    m_configIDMap.insert(CK_SimpleFocUart_DataBits,              	{"simplefoc_uart",           "data_bits",                "8"});
    m_configIDMap.insert(CK_SimpleFocUart_StopBits,              	{"simplefoc_uart",           "stop_bits",                "1"});
    m_configIDMap.insert(CK_SimpleFocUart_Parity,                	{"simplefoc_uart",           "parity",                   "None"});

    m_configIDMap.insert(CK_ElectronicScaleLiChenUart_AutoOpen,     {"electronicscalelichen_uart",    		"auto_open",                false});
    m_configIDMap.insert(CK_ElectronicScaleLiChenUart_Name,         {"electronicscalelichen_uart",           "name",                     "COM4"});
    m_configIDMap.insert(CK_ElectronicScaleLiChenUart_BaudRate,     {"electronicscalelichen_uart",           "baud_rate",                "115200"});
    m_configIDMap.insert(CK_ElectronicScaleLiChenUart_DataBits,     {"electronicscalelichen_uart",           "data_bits",                "8"});
    m_configIDMap.insert(CK_ElectronicScaleLiChenUart_StopBits,     {"electronicscalelichen_uart",           "stop_bits",                "1"});
    m_configIDMap.insert(CK_ElectronicScaleLiChenUart_Parity,       {"electronicscalelichen_uart",           "parity",                   "None"});

    m_configIDMap.insert(CK_ElectronicScaleShimadzuUart_AutoOpen,   {"electronicscaleshimadzu_uart",    		"auto_open",                false});
    m_configIDMap.insert(CK_ElectronicScaleShimadzuUart_Name,       {"electronicscaleshimadzu_uart",           "name",                     "COM4"});
    m_configIDMap.insert(CK_ElectronicScaleShimadzuUart_BaudRate,   {"electronicscaleshimadzu_uart",           "baud_rate",                "115200"});
    m_configIDMap.insert(CK_ElectronicScaleShimadzuUart_DataBits,   {"electronicscaleshimadzu_uart",           "data_bits",                "8"});
    m_configIDMap.insert(CK_ElectronicScaleShimadzuUart_StopBits,   {"electronicscaleshimadzu_uart",           "stop_bits",                "1"});
    m_configIDMap.insert(CK_ElectronicScaleShimadzuUart_Parity,     {"electronicscaleshimadzu_uart",           "parity",                   "None"});
}

void SystemConfig::restoreDefault()
{
    set(CK_Uart_AutoOpen, 			    			false);
    set(CK_Uart_Name, 							    "COM4");
    set(CK_Uart_BaudRate, 						    "115200");
    set(CK_Uart_DataBits, 						    "8");
    set(CK_Uart_StopBits, 						    "1");
    set(CK_Uart_Parity, 						    "None");

    set(CK_DebugUart_AutoOpen, 						false);
    set(CK_DebugUart_Name, 							"COM2");
    set(CK_DebugUart_BaudRate, 						"115200");
    set(CK_DebugUart_DataBits, 						"8");
    set(CK_DebugUart_StopBits, 						"1");
    set(CK_DebugUart_Parity, 						"None");

    for (int i = 0; i < 4; i++)
    {
        int offset = 6 * i;
        set((ConfigKey)(CK_PumpAUart_AutoOpen + offset), 					false);
        set((ConfigKey)(CK_PumpAUart_Name + offset), 						"COM4");
        set((ConfigKey)(CK_PumpAUart_BaudRate + offset), 					"115200");
        set((ConfigKey)(CK_PumpAUart_DataBits + offset), 					"8");
        set((ConfigKey)(CK_PumpAUart_StopBits + offset), 					"1");
        set((ConfigKey)(CK_PumpAUart_Parity + offset), 						"None");
    }

    set(CK_DetectorUart_AutoOpen, 						false);
    set(CK_DetectorUart_Name, 							"COM4");
    set(CK_DetectorUart_BaudRate, 						"115200");
    set(CK_DetectorUart_DataBits, 						"8");
    set(CK_DetectorUart_StopBits, 						"1");
    set(CK_DetectorUart_Parity, 						"None");
    set(CK_DetectorUart_Parity, 						"None");

    set(CK_CollectorUart_AutoOpen, 						false);
    set(CK_CollectorUart_Name, 							"COM4");
    set(CK_CollectorUart_BaudRate, 						"115200");
    set(CK_CollectorUart_DataBits, 						"8");
    set(CK_CollectorUart_StopBits, 						"1");
    set(CK_CollectorUart_Parity, 						"None");
    set(CK_CollectorUart_Parity, 						"None");

    set(CK_AutoSamplerUart_AutoOpen, 					false);
    set(CK_AutoSamplerUart_Name, 						"COM4");
    set(CK_AutoSamplerUart_BaudRate, 					"115200");
    set(CK_AutoSamplerUart_DataBits, 					"8");
    set(CK_AutoSamplerUart_StopBits, 					"1");
    set(CK_AutoSamplerUart_Parity, 						"None");
    set(CK_AutoSamplerUart_Parity, 						"None");

    set(CK_SimpleFocUart_AutoOpen, 						false);
    set(CK_SimpleFocUart_Name, 							"COM4");
    set(CK_SimpleFocUart_BaudRate, 						"115200");
    set(CK_SimpleFocUart_DataBits, 						"8");
    set(CK_SimpleFocUart_StopBits, 						"1");
    set(CK_SimpleFocUart_Parity, 						"None");
    set(CK_SimpleFocUart_Parity, 						"None");

    set(CK_ElectronicScaleLiChenUart_AutoOpen, 						false);
    set(CK_ElectronicScaleLiChenUart_Name, 							"COM4");
    set(CK_ElectronicScaleLiChenUart_BaudRate, 						"115200");
    set(CK_ElectronicScaleLiChenUart_DataBits, 						"8");
    set(CK_ElectronicScaleLiChenUart_StopBits, 						"1");
    set(CK_ElectronicScaleLiChenUart_Parity, 						"None");
    set(CK_ElectronicScaleLiChenUart_Parity, 						"None");

    set(CK_ElectronicScaleShimadzuUart_AutoOpen, 						false);
    set(CK_ElectronicScaleShimadzuUart_Name, 							"COM4");
    set(CK_ElectronicScaleShimadzuUart_BaudRate, 						"115200");
    set(CK_ElectronicScaleShimadzuUart_DataBits, 						"8");
    set(CK_ElectronicScaleShimadzuUart_StopBits, 						"1");
    set(CK_ElectronicScaleShimadzuUart_Parity, 						"None");
    set(CK_ElectronicScaleShimadzuUart_Parity, 						"None");
}

bool SystemConfig::init()
{
    bool isOk = IniConfig::init();
//    isOk = false;
    if (!isOk)
    {
        // 如果初始化没成功，则恢复默认设置
        // 当然如果有备份，可以先恢复备份设置，这里就先只是恢复默认设置了

        qDebug() << "Restore default system config";
        restoreDefault();
    }
    else
    {
        // 初始化成功
    }
    return isOk;
}

bool SystemConfig::get(ConfigKey key, QVariant &value)
{
    IniConfigID id = m_configIDMap.value(key);
    if (id.isNull())
        return false;
    QString iniKey = id.sector + '/' + id.key;
    bool ret = IniConfig::get(iniKey, value);
    if (!value.isValid())
        value = id.defult;
//    qDebug() << iniKey << value;
    return ret;
}

bool SystemConfig::set(ConfigKey key, const QVariant &value)
{
    IniConfigID id = m_configIDMap.value(key, IniConfigID());
    if (id.isNull())
        return false;
    QString iniKey = id.sector + '/' + id.key;
    return IniConfig::set(iniKey, value);
}

QList<ConfigKey> SystemConfig::allKeys()
{
    return m_configIDMap.keys();
}
